package mdrtu;

import com.serotonin.io.serial.SerialParameters;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;
import com.serotonin.modbus4j.msg.WriteCoilRequest;
import com.serotonin.modbus4j.msg.WriteCoilResponse;
import com.serotonin.modbus4j.msg.WriteRegisterRequest;
import com.serotonin.modbus4j.msg.WriteRegisterResponse;

import entity.ConnectionInfor;

public class re_6 {
	public static String commPortId;
	public static int baudRate; 
	public static int dataBits;
	public static int stopBits;
	public static int parity;
	public static int slaveId;
	public static int offset;
	public static int value;
	public re_6(ConnectionInfor conninfor)
	{
		commPortId = "COM"+conninfor.getStream_control(); 
		 baudRate =Integer.parseInt( conninfor.getBaud_rate()); 
		 dataBits = Integer.parseInt( conninfor.getData_bits());
		 stopBits = Integer.parseInt( conninfor.getStop_bit());
		 parity = Integer.parseInt(conninfor.getOdd_even_check());
		 slaveId = conninfor.getModel_address();
		 
		 offset = conninfor.getRegister_address();
		
		 value  =conninfor.getValue();
	};
	public static int remain1(){
		int result;
		SerialParameters serialParameters = serialParaSet(commPortId, baudRate, dataBits,  stopBits,  parity);
		ModbusFactory modbusFactory = new ModbusFactory();

        ModbusMaster master = modbusFactory.createRtuMaster(serialParameters);
        try {
			master.init();
		} catch (ModbusInitException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       result = writeRegisterTest(master, slaveId, offset, value);
        master.destroy();
        return result;
	};
	private static SerialParameters serialParaSet(String commPortId3, int baudRate3, int dataBits3, int stopBits3, int parity3) {
		SerialParameters serialParameters = new SerialParameters();		
		serialParameters.setCommPortId(commPortId3);//鐠佹儳鐣綧ODBUS闁俺顔嗛惃鍕鐞涘苯褰�	
		serialParameters.setParity(parity3);//鐠佹儳鐣鹃幋鎰￥婵傚洤浼撻弽锟犵崣		
		serialParameters.setDataBits(dataBits3);//鐠佹儳鐣鹃幋鎰殶閹诡喕缍呴弰锟�娴ｏ拷		
		serialParameters.setStopBits(stopBits3);//鐠佹儳鐣炬稉锟�娑擃亜浠犲顫秴
		serialParameters.setPortOwnerName("Numb nuts");		
		serialParameters.setBaudRate(baudRate3);//娑撹尪顢戦崣锝勭瑐閻ㄥ嫭灏濋悧鍦芳
		return serialParameters;
	}
	 public static int writeRegisterTest(ModbusMaster master, int slaveId3, int offset3, int value3) {
	     int result1 = 0;  
		 try {
	            WriteRegisterRequest request = new WriteRegisterRequest(slaveId3, offset3, value3);
	            WriteRegisterResponse response = (WriteRegisterResponse) master.send(request);

	            if (response.isException())
	                {
	            	System.out.println("Exception response: message=" + response.getExceptionMessage());
	                result1 = 0;
	                }
	            else
	                {
	            	System.out.println("Success");
	            	result1 = 1;
	                }
	        }
	        catch (ModbusTransportException e) {
	            e.printStackTrace();
	        }
		 return result1;
	    }
}
